package domain

import (
	"context"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"listenboos-common/db"
	"listenboos-common/tools"
	"time"
	"ucenter/dao"
	"ucenter/model"
	"ucenter/repo"
)

type UserAccountDomain struct {
	userAccount repo.UserAccountRepo
}

func (u *UserAccountDomain) FindUserAccount(ctx context.Context, userId int64) (account *model.UserAccount, err error) {
	account, err = u.userAccount.FindUserAccount(ctx, userId)
	if err != nil {
		logx.Error(err)
		return nil, errors.New("数据库异常")
	}
	return account, err
}
func (u *UserAccountDomain) CreateUserAccount(ctx context.Context, userId int64) error {
	var account *model.UserAccount
	account.UserId = userId
	account.AvailableAmount = "0"
	account.LockAmount = "0"
	account.TotalAmount = "0"
	account.TotalIncomeAmount = "0"
	account.TotalPayAmount = "0"
	now := tools.ToTimeString(time.Now().UnixMilli())
	account.CreateTime = now
	account.UpdateTime = now
	err := u.userAccount.CreateUserAccount(ctx, account)
	if err != nil {
		logx.Error(err)
		return errors.New("数据库异常")
	}
	return err
}
func NewUserAccountDomain(db *db.ListenDB) *UserAccountDomain {
	return &UserAccountDomain{
		userAccount: dao.NewUserAccountDao(db),
	}
}
